<?php

class Place {

    //General
    public $name;
    public $rating;
    public $phone;
    
    //CityGrid
    public $citygrid_id;
    public $citygrid_image;
    public $website;

    //Facebook
    public $facebook_page_id;
    public $facebook_fan_count;
    public $facebook_categories;
    public $facebook_likes;
    
    //Yelp
    public $yelp_id;
    public $yelp_url;
    public $yelp_image_url;
    public $yelp_rating_image_url;
    public $yelp_snippet_text;
    public $yelp_review_count;
    
    public $distance;
    public $number_of_locations;
    
    public $outrank_score_facebook;
    public $outrank_score_overall;
    public $is_yelp = false;
    public $is_citygrid = false;
    public $is_facebook = false;

    // toString overridden
    public function __toString() {
        return str_replace(array('\'', ' ', '-'), '', $this->name);
    }

    public function matches($place) {
        return str_replace(array('\'', ' ', '-'), '', $this->name) == str_replace(array('\'', ' ', '-'), '', $place->name);
    }

    public function isYelp() {
        return $this->is_yelp;
    }

    public function isCityGrid() {
        return $this->is_citygrid;
    }

    public function isFacebook() {
        return $this->is_facebook;
    }

    /**
     * Combines the instance with a supplied Place.
     * @param type $place
     */
    public function combineWith($place) {
        if ($this->isYelp()) {
            $this->citygrid_id = $place->citygrid_id;
            $this->facebook_page_id = $place->facebook_page_id;
        } elseif ($this->isCityGrid()) {
            $this->yelp_id = $place->yelp_id;
            $this->facebook_page_id = $place->facebook_page_id;
        } else {
            $this->citygrid_id = $place->citygrid_id;
            $this->yelp_id = $place->yelp_id;
        }
    }

    public function calculateFacebookOutRankScore() {
        return 1;
    }

    public function calculateOverallOutRankScore() {
        return 1;
    }

    public function add() {
        $con = pg_connect("host=localhost port=5432 dbname=foggy user=postgres password=pickles") or die("Error in connection: " . pg_last_error());

        // prepare query
        $stmt = pg_prepare($con, "ps", "INSERT INTO foggy.places (name) VALUES ($1)");
        // execute query
        $result = pg_execute($con, "ps", array($this->name)) or die("Error in SQL query (name '$this->name'): " . pg_last_error());

        // free memory
        pg_free_result($result);

        // close connection
        pg_close($con);
        return $result;
    }

}

function getAllPlaces() {
    $con = pg_connect("host=localhost port=5432 dbname=foggy user=postgres password=pickles") or die("Error in connection: " . pg_last_error());

    $sql = "SELECT * FROM foggy.places";
    $result = pg_query($con, $sql);

    // iterate over result set
    // print each row
    while ($row = pg_fetch_array($result)) {
        echo "Name: " . $row['name'] . "<br />";
    }

    // free memory
    pg_free_result($result);

    // close connection
    pg_close($con);
}

?>
